<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        姓名<input type="text" v-model="newStu.name">
        成绩<input type="text" v-model.number="newStu.score" @keyup.enter="onAdd">
        <button @click="onAdd">添加</button>
        <br>
        <!-- sort() -->
        <button @click="onSort">排序</button>
        <ul v-for="stu in stuList">
            <li>{{stu.name}}:{{stu.score}}</li>
        </ul>
    </div>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        var app = Vue.createApp({
            data: function () {
                return {
                    stuList: [
                        { name: '嘉远', score: 80 },
                        { name: '志文', score: 89 },
                        { name: '周飘', score: 90 },
                    ],
                    newStu:
                    {
                        name: '',
                        score:''
                    }

                }

            },
            methods: {
                onSort() {
                    this.stuList.sort((a, b) => b.score - a.score);//相减得到一个正数或者负数
                },
                onAdd(){
                    //如果姓名没写，不添加
                    if(this.newStu.name==''){
                        alert('姓名不能为空');
                        return;
                    }
                    if(this.newStu.score=='') this.newStu.score=0;
                    //添加 复制对象
                    /* let ne = {
                        name:this.newStu.name,
                        score:this.newStu.score
                    } */

                    //es6中复制对象的方式
                    this.stuList.push({...this.newStu});
                }
            },
        }).mount('#app')
    </script>
</body>

</html>